package com.dslu.fx.listener;

import com.alibaba.fastjson.JSON;
import com.dslu.fx.dao.EmergencyDepartDao;
import com.dslu.fx.domain.CarDO;
import com.dslu.fx.domain.EmergencyDO;
import com.dslu.fx.domain.EmergencyDepartDO;
import com.dslu.fx.service.DzpService;
import com.dslu.fx.vo.BigDzpChildrenVO;
import com.dslu.fx.vo.BigDzpVO;
import com.dslu.fx.vo.DateUtil;
import com.dslu.fx.vo.EmergencyStockVO;
import onbon.y2.Y2Exception;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DzpListener {

    @Autowired
    private DzpService dzpService;

    @Autowired
    private EmergencyDepartDao emergencyDepartDao;

    // 通过注解自动创建 spring.simple.queue 队列
    @RabbitListener(queuesToDeclare = @Queue("dzp.stock"))
    public void listen(String msg) throws Y2Exception {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //System.out.println("简单队列 接收到消息：" + msg);
        List<Integer> strings = JSON.parseArray(msg, Integer.class);
        for (Integer areaId:strings){
            dzpService.flushDzpByAreaId(areaId);
        }

    }


    @RabbitListener(queuesToDeclare = @Queue("dzp.emergency"))
    public void emergencyListen(String emergencyIdStr) throws Y2Exception {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("紧急出库ID：" + emergencyIdStr);
        Integer emergencyIds = Integer.valueOf(emergencyIdStr);
        List<Integer> areaIds = emergencyDepartDao.getAreaIds(emergencyIds);
        Map map=new HashMap();
        map.put("emergencyId",emergencyIds);
        for (Integer areaId:areaIds){
            map.put("areaId",areaId);
            dzpService.flushEmergencyDzp(map);
        }
    }


    @RabbitListener(queuesToDeclare = @Queue("dzp.big.emergency"))
    public void bigEmergencyListen(String mapStr) throws Y2Exception, IOException {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("紧急出库数据：" + mapStr);
        Map param = JSON.parseObject(mapStr, Map.class);
        Integer type = Integer.valueOf(param.get("type") + "");
        Integer id = Integer.valueOf(param.get("id") + "");

        EmergencyDO emergency = emergencyDepartDao.getEmergencyById(id);
        List<EmergencyStockVO> emergencyStockList=null;
        List<CarDO> carList=null;
        if(type==1){
            Map ifMap=new HashMap();
            ifMap.put("emergencyTemplateId",emergency.getEmergencyTemplateId());
            ifMap.put("status","0");
            emergencyStockList = emergencyDepartDao.getEmergencyTemplateStockList(ifMap);
            carList=emergencyDepartDao.getTemplateCarList(ifMap);
        }else if(type==2){
            Map ifMap=new HashMap();
            ifMap.put("emergencyId",id);
            emergencyStockList = emergencyDepartDao.getEmergencyStockVOList(ifMap);
            carList=emergencyDepartDao.getCarList(ifMap);
        }

        BigDzpVO bigDzpVO=new BigDzpVO();
        bigDzpVO.setBatchNo(emergency.getBatchNo());
        bigDzpVO.setTime(DateUtil.formatDateTime(emergency.getCreateTime()));
        String cars = carList.stream().map(w -> (w.getNumber())).collect(Collectors.joining("，"));
        bigDzpVO.setCars(cars);

        Map<Integer, List<EmergencyStockVO>> groupMap = emergencyStockList.stream().collect(Collectors.groupingBy(EmergencyStockVO::getDepartId));


        Map ifMap1=new HashMap();
        ifMap1.put("emergencyId",id);

        List<BigDzpChildrenVO> list=new ArrayList<>();
        for (Integer departId:groupMap.keySet()){
            ifMap1.put("departId",departId);
            EmergencyDepartDO one = emergencyDepartDao.getOne(ifMap1);
            List<EmergencyStockVO> emergencyStockVOS = groupMap.get(departId);
            EmergencyStockVO emergencyStockVO = emergencyStockVOS.get(0);
            String prodNames = emergencyStockVOS.stream().map(w -> (w.getName())).collect(Collectors.joining("，"));
            BigDzpChildrenVO bigDzpChildrenVO=new BigDzpChildrenVO();
            bigDzpChildrenVO.setProdName(prodNames);
            if(one.getStatus()==0){
                bigDzpChildrenVO.setStatus("待出库");
            }else if(one.getStatus()==1){
                bigDzpChildrenVO.setStatus("已出库");
            }else if(one.getStatus()==2){
                bigDzpChildrenVO.setStatus("已回库");
            }

            bigDzpChildrenVO.setDepartName(emergencyStockVO.getDepartName());
            bigDzpChildrenVO.setCategoryName(emergencyStockVO.getAreaName());
            list.add(bigDzpChildrenVO);
        }
        bigDzpVO.setList(list);


        dzpService.flushBigEmergencyDzp(bigDzpVO);
    }

}
